package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/commons/page"
	cmodel "ksd-social-api/modules/special/model"
	"strconv"
)

/**
 * 用户购买专题
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type UserBuySpecialBindMapper struct {
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询小册和code是否存在
 */
func (mapper *UserBuySpecialBindMapper) CountSpecialBindCode(code string, specialBindid uint64, systemId uint64) int {
	db := orm.NewOrm()
	var total int
	err := db.Raw("select count(1) from xk_special_bind_code where opid = ? and code = ? and mark = 0 and system_id = ?", specialBindid, code, systemId).QueryRow(&total)
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据code和小册id删除兑换码
 */
func (mapper *UserBuySpecialBindMapper) DelSpecialBindCode(code string, specialBindid uint64, systemId uint64) bool {
	db := orm.NewOrm()
	_, err := db.Raw("delete from xk_special_bind_code where opid = ? and code = ? and mark = 0 and system_id = ?", specialBindid, code, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 标识兑换码已被使用
 */
func (mapper *UserBuySpecialBindMapper) UpdateSpecialBindCode(code string, specialBindid uint64, systemId uint64) bool {
	db := orm.NewOrm()
	_, err := db.Raw("update xk_special_bind_code set mark = 1 where opid = ? and code = ? and system_id = ?", specialBindid, code, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买小册信息
 */
func (service *UserBuySpecialBindMapper) SaveUserBuySpecialBindCoin(userBuySpecialBind cmodel.SpecialBindUserBuy) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		price, _ := strconv.Atoi(userBuySpecialBind.Price)
		// 保存用户小册订单
		_, err = txOrm.Insert(&userBuySpecialBind)
		// 同步购买和订阅的用户数量
		_, err = txOrm.Raw("update xk_special set buy_num = buy_num + 1  where id = ? and buy_num >= 0 and system_id = ?", userBuySpecialBind.Opid, userBuySpecialBind.SystemId).Exec()
		// 同时学习币
		_, err = txOrm.Raw("update xk_user set coin = coin - ?  where id = ? and coin >= ? and system_id = ?", price, userBuySpecialBind.UserId, price, userBuySpecialBind.SystemId).Exec()
		return err
	})
	return err == nil
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买专题信息
 */
func (service *UserBuySpecialBindMapper) SaveUserBuySpecialBind(userUserBuySpecialBind cmodel.SpecialBindUserBuy) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ct context2.Context, txOrm orm.TxOrmer) (err error) {
		// 保存用户专题订单
		_, err = txOrm.Insert(&userUserBuySpecialBind)
		// 同步购买和订阅的用户数量
		_, err = txOrm.Raw("update xk_special set buy_num = buy_num + 1  where id = ? and buy_num >= 0 and system_id = ?", userUserBuySpecialBind.Opid, userUserBuySpecialBind.SystemId).Exec()
		return err
	})
	return err == nil
}

/**
 * 查询用户是否购买专题
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *UserBuySpecialBindMapper) CountUserBuySpecialBind(userId uint64, specialId uint64, systemId uint64, payMethod int) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_special_bind_user_buy WHERE user_id  = ? AND opid = ? and pay_method = ? and system_id = ?", userId, specialId, payMethod, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * 查询用户是否购买专题
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *UserBuySpecialBindMapper) CountUserBuySpecialBindNo(userId uint64, specialId uint64, systemId uint64) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_special_bind_user_buy WHERE user_id  = ? AND opid = ? and system_id = ?", userId, specialId, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * 统计专题购买人数
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *UserBuySpecialBindMapper) CountUserBuySpecialBindId(specialId uint64, systemId uint64) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_special_bind_user_buy WHERE opid = ? and system_id = ?", specialId, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 我学习的专题
 */
func (mapper *UserBuySpecialBindMapper) FindMySpecialBindsOrderPage(userId uint64, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	userBuySpecialBindList := []cmodel.SpecialBindUserBuy{}
	//创建orm对象
	mysql := orm.NewOrm()
	var total int64 = 0
	err1 := mysql.Raw(`
		select 
			count(1)
		from xk_special_bind_user_buy 
		where user_id = ? and system_id = ?
	`).SetArgs(userId, systemId).QueryRow(&total)
	if err1 != nil {
		return nil, err
	}
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 分页查询
	_, err2 := mysql.Raw(`
		select 
			id,
			user_id,
			opid,
			create_time,
			update_time,
			nickname,
			avatar,
			title,
			cover,
			description,
			code,
			price,
			phone,
			username,
			address,
			orderno,
			uuid,
			order_json,
			tradeno,
			pay_method,
			pay_method_name
		from xk_special_bind_user_buy 
		where user_id = ? and system_id = ?
		order by create_time desc
		limit ?,?
	`).SetArgs(userId, systemId, page.CurrentPage, page.PageSize).QueryRows(&userBuySpecialBindList)

	// 执行count查询
	if err1 != nil {
		return nil, err
	}
	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = userBuySpecialBindList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}
